#include "tools.h"
#include <QStringList>
#include <QObject>

SSTools::SSTools()
{

}

SSTools::~SSTools()
{

}

QString SSTools::PriceRemoveZero(const QString& szSrc, int nZeroNum) {
	QString szResult = szSrc;
	int nLen = szSrc.length();
	if (nLen <= 0)return szResult;//空字符
	int nPos = szResult.indexOf('.');
	if (nPos >= 0) {//小数
		// QB-9802 修复当输入100.时，变成1的问题，注释此处
		//if(nPos == nLen - 1){//小数点在最后
		//	szResult = szResult.Left(nLen - 1);
		//}
		if (nPos == 0) {//小数点前补零
			szResult = "0" + szResult;
		}

		QString szTemp = szResult.right(1);
		while (szTemp == "0") {
			szResult = szResult.left(szResult.length() - 1);
			szTemp = szResult.right(1);
		}
	}
	if (nZeroNum > 0) {
		//小数点后补零
		float fvalue = (float)szResult.toFloat();
		if (qAbs(fvalue) < 0.00001)
			szResult = "0";
		else {
			nPos = szResult.indexOf('.');
			if (nPos < 0) {
				szResult += ".";
				for (int i = 0; i < nZeroNum; i++)szResult += "0";
			}
			else {
				while (nPos > szResult.length() - nZeroNum - 1) {
					szResult += "0";
				}
			}
		}
	}
	else if (nZeroNum == 0) {
		if (szResult.right(1) == ".")
			szResult = szResult.left(szResult.length() - 1);
	}
	return szResult;
}

std::string SSTools::PriceRemoveZero(const char* sSrc, int nZeroNum) {
	QString str = sSrc;
	str = PriceRemoveZero(str, nZeroNum);
	std::string sResult = str.toStdString();
	return sResult;
}

QString SSTools::StringRemoveZero(const QString& str, bool bNeedFormat)
{
	QString szResult = "";
	int nLen0 = str.length();
	if (nLen0 <= 0)
	{
		szResult = "--";
		return szResult;
	}

	QStringList strs = str.split("+");
	for (int i = 0; i < strs.size(); ++i)
	{
		QString res(strs[i]);
		if (strs[i].toFloat() < 0.0001)
			res = "--";
		else if (bNeedFormat)
			res = PriceRemoveZero(strs[i], 0);

		if (szResult.isEmpty())
			szResult = res;
		else
			szResult.append(" + ").append(res);
	}

	return szResult;
}

QString SSTools::FormatePrice(const char* ctPrice)
{
	if (ctPrice && (qstricmp(ctPrice, "bid") == 0 || qstricmp(ctPrice, "ofr") == 0))
	{
		return QString::fromLocal8Bit(ctPrice);
	}
	else
	{
		// 小于2位补足两位,超过2位,则保持不变
		int len = ctPrice ? strlen(ctPrice) : 0;
		const char* pos = ctPrice ? strchr(ctPrice, '.') : 0;
		int nPos = pos ? (pos - ctPrice) : -1;
		if (len > 0)
		{
			if (nPos == -1 || nPos + 2 >= len)
			{
				double dbPrice = atof(ctPrice);
				return QString::number(dbPrice, 'f', 2);
			}
			else
			{
				return QString::fromLocal8Bit(ctPrice);
			}
		}
		else
		{
			return "";
		}
	}
	return "";
}

QString SSTools::int2Time(unsigned int uiTime, int Formart/*0,'-',1,'/'*/)//int型时间转string
{
	QString strTime = "";
	if (uiTime == 0) return strTime;
	int nYear = uiTime / 10000;
	int nMonth = (uiTime % 10000) / 100;
	int nDay = uiTime % 100;
	if (Formart == 1)  // 0 = yyyy-mm-dd , 1 = yyyy/mm/dd
		strTime = QString("%1/%2/%3").arg(nYear).arg(nMonth, 2, 10, QChar('0')).arg(nDay, 2, 10, QChar('0'));
	else if (Formart == 0)
		strTime = QString("%1-%2-%3").arg(nYear).arg(nMonth, 2, 10, QChar('0')).arg(nDay, 2,10, QChar('0'));
	return strTime;
}

int SSTools::getSemiQuote(const char* rebate, const char* return_point, const char* sPrice, QString& strQuote)
{//任何情况下，不再显示返点
	return -1;
	int nType = -1;
	strQuote = "";
	if (atoi(rebate) == 1)
	{
		if (strcmp(return_point, "") == 0)
		{
			if (strcmp(sPrice, "Bid") == 0 || strcmp(sPrice, "Ofr") == 0 || strcmp(sPrice, "") == 0)
			{
				nType = 0;
				strQuote = QObject::tr("平价返");//_T("平价返");
			}
			else
			{
				nType = 1;
				strQuote = "F--";
			}
		}
		else
		{
			
			nType = 1;
			QString strTemp = FormatePrice(return_point);
			strQuote = "F" + strTemp;
		}
	}

	return nType;
}
